package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.framework.exception.BizException;
import com.molichuxing.services.infrastructure.bean.DealerDetailBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.DealerDetailMapper;
import com.molichuxing.services.infrastructure.dto.request.create.DealerDetailCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.DealerDetailModifyDto;
import com.molichuxing.services.infrastructure.dto.response.DealerDetailDto;
import com.molichuxing.services.infrastructure.service.DealerDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 经销商详情信息表 服务实现类
 * </p>
 *
 * @author zph
 * @since 2019-08-12
 */
@Service("dealerDetailService")
public class DealerDetailServiceImpl implements DealerDetailService {


    @Autowired
    DealerDetailMapper dealerDetailMapper;


    /**
     * 通过id获取经销商详情信息
     *
     * @param ids
     * @return
     */
    @Override
    public Map<Integer, DealerDetailDto> getDealerDetailMapByIds(List<Integer> ids) throws Exception {
        List<DealerDetailBean> dealerDetailList = dealerDetailMapper.getBatchByIds(ids);
        return Convert.toDealerDetailDtoMap(dealerDetailList);
    }

    /**
     * 通过id获取经销商详情信息
     *
     * @param ids
     * @return
     */
    @Override
    public List<DealerDetailDto> getDealerDetailListByIds(List<Integer> ids) throws Exception {
        List<DealerDetailBean> dealerDetailList = dealerDetailMapper.getBatchByIds(ids);
        return Convert.toDealerDetailDtoList(dealerDetailList);
    }

    /**
     * 经销商详情添加
     *
     * @param dealerDetailCreateDto
     * @return
     * @throws Exception
     */
    @Override
    public Integer createDealerDetail(DealerDetailCreateDto dealerDetailCreateDto) throws Exception {
        Assert.notNull(dealerDetailCreateDto, "经销商添加失败");

        DealerDetailBean dealerDetail = Convert.toDealerDetail(dealerDetailCreateDto);
        dealerDetailMapper.create(dealerDetail);
        return dealerDetail.getId();
    }

    /**
     * 经销商详情修改
     *
     * @param dealerDetailModifyDto
     * @return
     * @throws Exception
     */
    @Override
    public Boolean modifyDealerDetail(DealerDetailModifyDto dealerDetailModifyDto) throws Exception {
        Assert.notNull(dealerDetailModifyDto, "经销商不存在");
        Integer dealerDetailId = dealerDetailModifyDto.getDealerId();
        Assert.notNull(dealerDetailId, "经销商不存在");
        DealerDetailBean dealerDetail = dealerDetailMapper.getById(dealerDetailId);
        Assert.notNull(dealerDetail, "经销商不存在");

        String email = dealerDetailModifyDto.getEmail();
        String oldEmail = dealerDetail.getEmail();
        if (!email.trim().equals(oldEmail)) {
            Boolean emailExist = getEmailExist(email);
            if (emailExist) {
                throw new BizException("邮箱已存在");
            }
        }

        Convert.toDealerDetail(dealerDetailModifyDto, dealerDetail);
        return (dealerDetailMapper.modifyById(dealerDetail) > 0) ? true : false;
    }

    /**
     * 获取经销商详情
     *
     * @param dealerId
     * @return
     * @throws Exception
     */
    @Override
    public DealerDetailDto getDealerDetailById(Integer dealerId) throws Exception {
        DealerDetailBean dealerDetail = dealerDetailMapper.getById(dealerId);
        return Convert.toDealerDetailDto(dealerDetail);
    }

    /**
     * 邮箱是否存在
     *
     * @param email
     * @return
     */
    @Override
    public Boolean getEmailExist(String email) {
        int result = dealerDetailMapper.getCountByEmail(email);
        if (result > 0) {
            return true;
        }
        return false;
    }

    /**
     * 获取经销商详情集合
     *
     * @return
     */
    @Override
    public List<DealerDetailDto> getDealerDetailList() {
        List<DealerDetailBean> list = dealerDetailMapper.getList();
        return Convert.toDealerDetailDtoList(list);
    }


}